package com.gy.assistant.ui.meeting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import im.zego.zim.callback.ZIMCallCancelSentCallback;
import im.zego.zim.callback.ZIMCallEndSentCallback;
import im.zego.zim.callback.ZIMCallQuitSentCallback;
import im.zego.zim.entity.ZIMCallEndConfig;
import im.zego.zim.entity.ZIMCallEndedSentInfo;
import im.zego.zim.entity.ZIMCallQuitConfig;
import im.zego.zim.entity.ZIMCallQuitSentInfo;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.assistant.R;
import com.gy.assistant.ui.BaseActivity;
import com.gy.assistant.ui.meeting.adapter.InMeetingAdapter;
import com.gy.assistant.ui.meeting.adapter.CallData;

public class InMeetingActivity extends BaseActivity {

    String callID;
    String callerID;
    InMeetingAdapter adapter;
    public static void actionStart(Activity activity,String callID,String callerID) {
        Intent intent = new Intent(activity, InMeetingActivity.class);
        intent.putExtra("callID",callID);
        intent.putExtra("caller",callerID);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_meeting);
        callID = getIntent().getStringExtra("callID");
        callerID = getIntent().getStringExtra("caller");
        adapter = new InMeetingAdapter();
        initView();
        CallManager.sharedInstance().addMeetingDataChanged(changed);
        adapter.setDataAndNotify(CallManager.sharedInstance().getCurrentList(callID));
        adapter.setCallerID(callerID);
    }

    private CallManager.CallDataChanged changed = new CallManager.CallDataChanged() {

        @Override
        public void onMeetingUserDataChangedCallback(ArrayList<CallData> meetingDataList) {
            adapter.setDataAndNotify(meetingDataList);
        }
    };

    @Override
    protected void onDestroy() {
        CallManager.sharedInstance().removeMeetingDataChanged(changed);
        changed = null;
        super.onDestroy();
    }

    private void initView() {
        TextView tv_title = findViewById(R.id.tv_title);
        Button btn_end = findViewById(R.id.btn_end);
        Button btn_quit = findViewById(R.id.btn_quit);
        Button btn_invite = findViewById(R.id.btn_invite);
        Button btn_cancel = findViewById(R.id.btn_cancel);
        RecyclerView rv = findViewById(R.id.rv_user_list);
        tv_title.setText(String.format("%s的会议",callerID));
        btn_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CallManager.sharedInstance().callEnd(callID, new ZIMCallEndConfig(), new ZIMCallEndSentCallback() {
                    @Override
                    public void onCallEndSent(String callID, ZIMCallEndedSentInfo info, ZIMError errorInfo) {
                        toast(String.format("onCallEndSent,error:%s,error msg:%s",errorInfo.code,errorInfo.message));

                    }

                });
            }
        });

        btn_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CallManager.sharedInstance().callQuit(callID, new ZIMCallQuitConfig(), new ZIMCallQuitSentCallback() {
                    @Override
                    public void onCallQuitSent(String callID, ZIMCallQuitSentInfo info, ZIMError errorInfo) {
                        toast(String.format("onCallQuitSent,error:%s,error msg:%s",errorInfo.code,errorInfo.message));
                        MeetingEndedActivity.actionStart(InMeetingActivity.this,callerID,info.quitTime - info.createTime,info.quitTime-info.acceptTime);
                        finish();
                    }
                });
            }
        });

        btn_invite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MeetingInviteActivity.actionStart(InMeetingActivity.this,callID);
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> useridList = new ArrayList<>();
                for (CallData chooseDatum : adapter.chooseData) {
                    useridList.add(chooseDatum.getCallUserInfo().userID);
                }
                CallManager.sharedInstance().callCancel(useridList, callID, new ZIMCallCancelSentCallback() {
                    @Override
                    public void onCallCancelSent(String callID, ArrayList<String> errorInvitees, ZIMError errorInfo) {
                        if (errorInfo.code == ZIMErrorCode.SUCCESS){
                            toast("取消邀请成功");
                        }else {
                            toast(String.format("取消邀请失败,code:%s,message:%s",errorInfo.code,errorInfo.message));
                        }
                    }
                });
            }
        });
        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(this));
    }
}